<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入数据字典名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getfindAll">查询</el-button>
				</el-form-item>
			  <el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


		<!--列表-->
		<el-table :data="systems" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
      <el-table-column  prop="name" label="数据字典名" width="150" sortable>
      </el-table-column>
			<el-table-column prop="sn" label="编号" width="150" sortable>
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="primary" @click="systemDetail(scope.$index, scope.row)" >明细</el-button>
				</template>
			</el-table-column>
		</el-table>
    <!--数据字典  工具条-->
    <el-col :span="24" class="toolbar">
      <!--			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <el-drawer
        title="明细表"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <div>
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.keyword1" placeholder="请输入数据明细名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="systemAll">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="systemsAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-table :data="systemtwo">
          <el-table-column property="name" width="300" label="明细名"></el-table-column>
          <el-table-column label="操作" width="250">
            <template scope="scope">
              <el-button size="small" @click="systemsEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="DetailDel(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>


      <!--数据明细 工具条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next"
                       @current-change="systemCurrentChange"
                       :current-page="currentPage1"
                       :page-size="pageSize1"
                       :total="total1"
                       style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>



		<!--数据字典  编辑 和 添加界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="数据字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--数据明细  编辑 和 添加界面-->
    <el-dialog :title="title1" :visible.sync="systemFormVisible" :close-on-click-modal="false">
      <el-form :model="systemForm" label-width="80px" :rules="systemRules" ref="systemForm">
        <el-form-item label="明细名称" prop="name">
          <el-input v-model="systemForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="systemFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="systemSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'


	export default {
		data() {
			return {
			  typeid: null,
        table: false,
        dialog: false,
        loading: false,
        gridData: [],
        form: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
        formLabelWidth: '80px',
        timer: null,


			  tree: [],
        drawer: false,
				filters: {
          keyword: '',
          keyword1: ''
				},
        systems: [],
        systemtwo: [],
        /*employees: [],*/
				total: 0,
				total1: 0,
				currentPage: 1,
        pageSize: 5,

        currentPage1: 1,
        pageSize1: 5,
				listLoading: false,
				sels: [],//列表选中列
        title: "",
        title1: "",
				editFormVisible: false, // 编辑界面是否显示
        systemFormVisible: false, // 编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入部门编号', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入部门名称', trigger: 'blur' }
          ]
				},
        systemRules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ]
        },
				//数据字典编辑界面数据
				editForm: {
          id: null,
          sn: "",
          name: ""
				},
        //数据明细编辑界面数据
        systemForm: {
          name: ""
        },
			}
		},
		methods: {
      systemDetail(index,row){
        this.table = true;
        this.typeid = row.id;
        this.getSystemDetail();
      },
		  // 该机查询小bug处理
      getfindAll(){
        this.currentPage = 1;
        this.getSystemType();
      },
      // 该机查询小bug处理
      systemAll(){
        this.currentPage1 = 1;
        this.getSystemDetail();
      },


      handleClose(done) {
        if (this.loading) {
          return;
        }
        this.$confirm('确定要提交表单吗？')
            .then(_ => {
              this.loading = true;
              this.timer = setTimeout(() => {
                done();
                // 动画关闭需要一定的时间
                setTimeout(() => {
                  this.loading = false;
                }, 400);
              }, 2000);
            })
            .catch(_ => {});
      },
      // 数据字典
			handleCurrentChange(val) {
        console.log(val)
				this.currentPage = val;
				this.getSystemType();
			},
      // 数据明细
      systemCurrentChange(val) {
        this.currentPage1 = val;
        this.getSystemDetail();
      },


			//获取数据字典目录
      getSystemType() {
        //发送Ajax请求后台获取数据  axios
				let para = {
				  currentPage : this.currentPage,
          keyword : this.filters.keyword,
          pageSize: this.pageSize
        };
				console.log(para)
				this.listLoading = true;  //显示加载圈
        this.$http.post("/systemType",para).then((res) => {
					this.total = res.data.totals;
					this.systems = res.data.rows;
					this.listLoading = false; // 关闭加载圈
				});
			},


      //获取数据名细
      getSystemDetail() {
        //发送Ajax请求后台获取数据  axios
        let para = {
          currentPage : this.currentPage1,
          keyword : this.filters.keyword1,
          // 链表id
          id : this.typeid,
          pageSize: this.pageSize1
        };

        this.listLoading = true;  //显示加载圈
        //NProgress.start();
        this.$http.post("/systemDetail",para).then((res) => {
          this.total1 = res.data.totals;
          // 连表 获取的id赋值
          this.systemtwo = res.data.rows;
          this.listLoading = false;
          //NProgress.done();
        });
      },
			//数据字典删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '友情提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/systemType/" + row.id)
            .then(result=>{
              this.listLoading = false; // 关闭加载圈
              if (result.data.success){
                this.$message({
                  message: "删除成功！",
                  type: 'success'
                });
              } else {
                this.$message({
                  message: result.data.message,
                  type: 'error'
                });
              }
              if ((this.total -1) % this.pageSize === 0){
                  this.currentPage = this.currentPage -1;
              }
              this.getSystemType();
            }).catch(result => {

          })
        })
			},
      // 明细删除
      DetailDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '友情提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.delete("/systemDetail/" + row.id)
              .then(result => {
                this.listLoading = false; // 关闭加载圈
                if (result.data.success) {
                  this.$message({
                    message: "删除成功！",
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                if ((this.total1 - 1) % this.pageSize1 === 0) {
                  this.currentPage1 = this.currentPage1 - 1;
                }
                this.getSystemDetail();
              }).catch(result => {

          })
        })
      },
			//数据字典显示编辑界面
			handleEdit: function (index, row) {
        this.title = "编辑"
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row); // 回显数据
        if (this.editForm.dirPath){
          let dirPath = this.editForm.dirPath;
          let arr = dirPath.split("/");
          let parr = [];
          for (let i = 1; i < arr.length -1; i++){
            parr.push(parseInt(arr[i]));
          }
          this.editForm.parent = parr;
        }
			},
			//数据字典显示新增界面
			handleAdd: function () {
        this.title = "新增"
				this.editFormVisible = true;
				this.editForm = {
          id: null,
          sn: "",
          name: ""
				};
			},
      //数据明细显示编辑界面
      systemsEdit: function (index, row) {
        this.title1 = "编辑"
        this.systemFormVisible = true;
        this.systemForm = Object.assign({}, row); // 回显数据
        if (this.systemForm.dirPath){
          let dirPath = this.systemForm.dirPath;
          let arr = dirPath.split("/");
          let parr = [];
          for (let i = 1; i < arr.length -1; i++){
            parr.push(parseInt(arr[i]));
          }
          this.systemForm.parent = parr;
        }
      },
      //数据明细显示新增界面
      systemsAdd: function () {
        this.title1 = "新增"
        this.systemFormVisible = true;
        this.systemForm = {
          name: ""
        };
      },

			//数据字典添加修改
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true; // 忙等框
							//NProgress.start();
							let para = Object.assign({}, this.editForm); // 克隆数据
              //  处理传输的数据格式
              if (para.parent){
                let pp = para.parent; // [1 , 2]
                let id = pp[pp.length -1] // 取数组最后一个值
                para.parent = {"id" : id}
              }

              this.$http.put("/systemType", para)
                .then(resule=> {
                  this.editLoading = false; // 关闭忙等框
                  if (resule.data.success) {
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    })
                  } else {
                    this.$message({
                      message: resule.data.message,
                      type: 'error'
                    })
                  }
                  this.editFormVisible = false; // 关闭模态框
                  this.getSystemType(); // 加载数据
                }).catch(result=>{
                  this.$message({
                    message:"系统故障，正在殴打程序员。。。。",
                    type: 'error'
                  })
              })
						})
					}
				});
			},

      //数据明细 添加修改
      systemSubmit: function () {
        this.$refs.systemForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true; // 忙等框
              //NProgress.start();
              let para = Object.assign({}, this.systemForm); // 克隆数据
              para.types_id = this.typeid;
              //  处理传输的数据格式
              if (para.parent){
                let pp = para.parent; // [1 , 2]
                let id = pp[pp.length -1] // 取数组最后一个值
                para.parent = {"id" : id}
              }

              this.$http.put("/systemDetail", para)
                  .then(resule=> {
                    this.editLoading = false; // 关闭忙等框
                    if (resule.data.success) {
                      this.$message({
                        message: '提交成功',
                        type: 'success'
                      })
                    } else {
                      this.$message({
                        message: resule.data.message,
                        type: 'error'
                      })
                    }
                    this.systemFormVisible = false; // 关闭模态框
                    this.getSystemDetail(); // 加载数据
                  }).catch(result=>{
                this.$message({
                  message:"系统故障，正在殴打程序员。。。。",
                  type: 'error'
                })
              })
            })
          }
        });
      },


			selsChange: function (sels) {
				this.sels = sels;
			},


      // 查询所有的员工
      /*getEmployees(){
			  this.$http.get("/employee")
          .then(result=>{
            this.employees = result.data
          }) .catch(result=>{

        })
      },*/
      /*// 查询部门数
      queryTree(){
			  this.$http.get("/systemType/tree")
          .then(result=>{
            this.tree = result.data
            console.debug(this.tree)
          }).catch(result=>{

        })
      },
      // 查询部门数
      queryTree1(){
        this.$http.get("/systemDetail/tree")
            .then(result=>{
              this.tree = result.data
              console.debug(this.tree)
            }).catch(result=>{

        })
      }*/

		},
		mounted() {
			this.getSystemType();
			//this.getSystemDetail();
		}
	}

</script>

<style scoped>

</style>